# 二叉树的中序遍历

# 树的遍历包括：前序、中序、后序

# 定义树的节点
class treeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

# 颜色标记法
class Solution:

    def inorderTraversal(self, root) -> list[int]:

        WHITE, GRAY = 0, 1

        res = []

        stack = [(WHITE, root)]

        while stack:
            # 取出一个节点及其颜色
            color, node = stack.pop()

            if node is None:
                continue
            # 如果需要修改前序、后序、中序，则从这里的顺序修改，类似于递归
            if color == WHITE:
                stack.append((WHITE, node.right))
                stack.append((GRAY, node))
                stack.append((WHITE, node.left))
            else:
                res.append(node.val)

        return res
    





